package com.shengwei.truck.driver.view.ui.home;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.shengwei.truck.driver.view.ui.map.MapFragment;
import com.shengwei.truck.driver.view.ui.mine.userinfo.MineFragment;
import com.shengwei.truck.driver.view.ui.task.TaskFragment;

/**
 * @author xw.gao
 * @version 2017/4/21
 * @git http://60.205.139.138:81/root/GSMServerAPP.git
 */
public class FragmentFactory {

    public static HomeFragment homeFragment = null;    //首页
    public static TaskFragment taskFragment = null;  //呼救列表
    public static MapFragment mapFragment = null;
    public static MineFragment mineFragment = null;


    /**
     * 根据底部导航的索引 获取对应的fragment的实例
     *
     * @param position
     * @return
     */
    public static Fragment getFragment(int position, FragmentManager fragmentManager) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();//首页
                    fragment = homeFragment;
                }
                else
                {
                    fragment = fragmentManager.findFragmentByTag(String.valueOf(position));
                    homeFragment = (HomeFragment) fragment;
                }

                break;
            case 1:
                if (taskFragment == null) {
                    taskFragment = new TaskFragment();//群聊
                    fragment = taskFragment;
                }
                else
                {
                    fragment = fragmentManager.findFragmentByTag(String.valueOf(position));
                    taskFragment = (TaskFragment) fragment;
                }
                break;
            case 2:
                if (mapFragment == null) {
                    mapFragment = new MapFragment();//记录
                    fragment = mapFragment;
                }
                else
                {
                    fragment = fragmentManager.findFragmentByTag(String.valueOf(position));
                    mapFragment = (MapFragment) fragment;
                }

                break;
            case 3:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();//我的
                    fragment = mineFragment;
                }
                else
                {
                    fragment = fragmentManager.findFragmentByTag(String.valueOf(position));
                    mineFragment = (MineFragment) fragment;
                }

                break;
        }
        return fragment;
    }
}
